/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mb.mbpart.memory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

/**
 *
 * @author thanh.nguyenchi
 */
public class NewsDataIndexer {
    private Directory directory;
    private String baseDir;

    public NewsDataIndexer(String baseDir) {
        this.directory = new RAMDirectory();
        this.baseDir = baseDir;
        try{            
            indexNewsData();
        }
        catch(Exception ex){
            System.out.println(ex.toString());
        }
    }
    
    public Directory getDirectory(){
        return this.directory;
    }

    public void indexNewsData() throws IOException {
        File d = new File(baseDir);
        IndexWriter writer = getWriter();
        indexNewsData(writer, d);
        writer.close();
    }

    private void indexNewsData(IndexWriter writer, File file) throws IOException {
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                indexNewsData(writer, f);
            }
        } else {
            String category = file.getParent().substring(baseDir.length()).replace(File.separatorChar, '/');
            Document doc = getDocument(category, file);
            writer.addDocument(doc);
        }
    }

    private IndexWriter getWriter() throws IOException {        
        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_45, analyzer);
        IndexWriter writer = new IndexWriter(directory, config);
        return writer;
    }

    private Document getDocument(String category, File file) throws IOException {
        Properties props = new Properties();
        props.load(new FileInputStream(file));
        Document doc = new Document();        
        return doc;
    }
}
